package com.situ.company.web220111.employee.service.Impl;

import com.situ.company.web220111.department.model.DepartmentModel;
import com.situ.company.web220111.department.service.IDepartmentService;
import com.situ.company.web220111.department.service.impl.DepartmentServiceImpl;
import com.situ.company.web220111.employee.dao.IEmployeeDao;
import com.situ.company.web220111.employee.dao.impl.EmployeeDaoImpl;
import com.situ.company.web220111.employee.model.EmployeeModel;
import com.situ.company.web220111.employee.service.IEmployeeService;
import com.situ.company.web220111.util.FmtEmpty;
import com.situ.company.web220111.util.FmtUpload;
import com.situ.company.web220111.util.MD5;

import java.util.List;

public class EmployeeServiceImpl implements IEmployeeService {

    private IEmployeeDao dao = new EmployeeDaoImpl();
    private static String passDefault = "123456";  // 设置一个初始密码

    @Override
    public String insert(EmployeeModel model) {
        EmployeeModel mdb = dao.selectModel(new EmployeeModel(model.getCode()));
        if (mdb != null) {
            return "repeat";  // 返回重复
        }
        String pass = model.getPass();
        if (FmtEmpty.isEmpty(pass))
            pass = passDefault;
        model.setPass(MD5.encode(pass));
        return dao.insert(model);
    }

    @Override
    public String deleteByCode(EmployeeModel model) {
        return deleteByCode(model);
    }

    @Override
    public String delete(EmployeeModel model) {
        return dao.delete(model);
    }

    @Override
    public String updateAll(EmployeeModel model) {
        return dao.updateAll(model);
    }

    @Override
    public String update(EmployeeModel model) {
        return dao.updateActive(model);
    }

    @Override
    public List<EmployeeModel> selectList(EmployeeModel model) {
        String code = model.getCode();
        model.setCode(FmtEmpty.isEmpty(code) ? "%" : "%" + code + "%");
        String name = model.getName();
        model.setName(FmtEmpty.isEmpty(name) ? "%" : "%" + name + "%");
        return dao.selectList(model);
    }

    @Override
    public EmployeeModel selectModel(EmployeeModel model) {
        return dao.selectModel(new EmployeeModel(model.getCode()));
    }

    @Override
    public String login(EmployeeModel model) {
        EmployeeModel mdb = selectModel(model);
        if (mdb == null)
            return "0";
        String pass = MD5.encode(model.getPass());
        return mdb.getPass().equals(pass) ? "1" : "2";
    }

    @Override
    public Integer selectCount(EmployeeModel model) {
        EmployeeModel model1 = new EmployeeModel();
        String code = model.getCode();
        model1.setCode(code == null ? "%" : "%" + code + "%");
        String name = model.getName();
        model1.setName(name == null ? "%" : "%" + name + "%");
        return dao.selectCount(model1);
    }

    @Override
    public String resetPass(EmployeeModel model) {
        String pass = passDefault;
        model.setPass(MD5.encode(pass));
        return dao.resetPass(model);
    }

    @Override
    public String updPass(EmployeeModel model) {
        if (FmtEmpty.isEmpty(model.getPass())){
            return "0";
        }
        model.setPass(MD5.encode(model.getPass()));
        return dao.updateActive(model);
    }

    @Override
    public String setDept(EmployeeModel model) {
        if (FmtEmpty.isEmpty(model.getCodeDept()))
            return "0";
        return dao.updateActive(model);
    }

    @Override
    public String delPic(EmployeeModel model) {
        EmployeeModel mdb = selectModel(model);
        String image = mdb.getImage();
        FmtUpload.delFile(image);
        model.setImage("");
        return update(model);
    }
}
